package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.*;
import com.ruoyi.zhgd.domain.vo.GroutingResultVo;
import com.ruoyi.zhgd.mapper.GroutingRecordMapper;
import com.ruoyi.zhgd.mapper.GroutingResultMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjGroutingReportEditBo;
import com.ruoyi.zhgd.domain.vo.ZjGroutingReportEditVo;
import com.ruoyi.zhgd.mapper.ZjGroutingReportEditMapper;
import com.ruoyi.zhgd.service.IZjGroutingReportEditService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 压浆报编辑信息保存Service业务层处理
 *
 * @author tll
 * @date 2023-12-08
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjGroutingReportEditServiceImpl implements IZjGroutingReportEditService {

    private final ZjGroutingReportEditMapper baseMapper;

    private final GroutingRecordMapper groutingRecordMapper;

    private final GroutingResultMapper groutingResultMapper;

    /**
     * 查询压浆报编辑信息保存
     */
    @Override
    public ZjGroutingReportEditVo queryById(String id){
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("主键ID不能为空");
        }
        // 根据id查询recording表的桩号和梁号
        GroutingRecord record = groutingRecordMapper.getById(id);
        QueryWrapper<ZjGroutingReportEdit> wrapper = new QueryWrapper<>();
        wrapper.eq("business_id", id);
        ZjGroutingReportEditVo vo = baseMapper.selectVoOne(wrapper);
        if (ObjectUtils.isNotEmpty(vo)) {
            vo.setBeamNumber(record.getBeamNumber());
            vo.setPileNumber(record.getPileNumber());
            return vo;
        } else {
            ZjGroutingReportEditVo data = new ZjGroutingReportEditVo();
            data.setBeamNumber(record.getBeamNumber());
            data.setPileNumber(record.getPileNumber());
            return data;
        }
    }

    /**
     * 查询压浆报编辑信息保存列表
     */
    @Override
    public TableDataInfo<ZjGroutingReportEditVo> queryPageList(ZjGroutingReportEditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjGroutingReportEdit> lqw = buildQueryWrapper(bo);
        Page<ZjGroutingReportEditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压浆报编辑信息保存列表
     */
    @Override
    public List<ZjGroutingReportEditVo> queryList(ZjGroutingReportEditBo bo) {
        LambdaQueryWrapper<ZjGroutingReportEdit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjGroutingReportEdit> buildQueryWrapper(ZjGroutingReportEditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjGroutingReportEdit> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getReportId()), ZjGroutingReportEdit::getReportId, bo.getReportId());
        lqw.eq(bo.getCreateDate() != null, ZjGroutingReportEdit::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateUser()), ZjGroutingReportEdit::getCreateUser, bo.getCreateUser());
        lqw.eq(bo.getUpdateDate() != null, ZjGroutingReportEdit::getUpdateDate, bo.getUpdateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateUser()), ZjGroutingReportEdit::getUpdateUser, bo.getUpdateUser());
        lqw.eq(StringUtils.isNotBlank(bo.getSnpzbh()), ZjGroutingReportEdit::getSnpzbh, bo.getSnpzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getWjj()), ZjGroutingReportEdit::getWjj, bo.getWjj());
        lqw.eq(StringUtils.isNotBlank(bo.getSnjcd()), ZjGroutingReportEdit::getSnjcd, bo.getSnjcd());
        lqw.eq(StringUtils.isNotBlank(bo.getMsl()), ZjGroutingReportEdit::getMsl, bo.getMsl());
        lqw.eq(StringUtils.isNotBlank(bo.getHjTemp()), ZjGroutingReportEdit::getHjTemp, bo.getHjTemp());
        lqw.eq(StringUtils.isNotBlank(bo.getJyTemp()), ZjGroutingReportEdit::getJyTemp, bo.getJyTemp());
        lqw.eq(StringUtils.isNotBlank(bo.getTgCondition()), ZjGroutingReportEdit::getTgCondition, bo.getTgCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getMjCondition()), ZjGroutingReportEdit::getMjCondition, bo.getMjCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), ZjGroutingReportEdit::getFileUrl, bo.getFileUrl());
        return lqw;
    }

    /**
     * 新增压浆报编辑信息保存
     */
    @Override
    public Boolean insertByBo(ZjGroutingReportEditBo bo) {
        ZjGroutingReportEdit add = BeanUtil.toBean(bo, ZjGroutingReportEdit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改压浆报编辑信息保存
     */
    @Override
    @Transactional
    public Boolean updateByBo(ZjGroutingReportEditBo bo) {
        if(ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主键ID不能为空");
        }
        // 根据id查询主表id 保存桩号和梁号 @TODO暂时不允许修改
//        GroutingRecord record = groutingRecordMapper.getById(bo.getId());
//        if (ObjectUtils.isNotEmpty(bo.getBeamNumber())) {
//            record.setBeamNumber(bo.getBeamNumber());
//            groutingRecordMapper.updateById(record);
//        }
//        if (ObjectUtils.isNotEmpty(bo.getPileNumber())) {
//            record.setPileNumber(bo.getPileNumber());
//            groutingRecordMapper.updateById(record);
//        }
        // 保存压浆编辑表
        QueryWrapper<ZjGroutingReportEdit> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("business_id", bo.getId());
        ZjGroutingReportEdit edit = baseMapper.selectOne(wrapper1);
        if (ObjectUtils.isNotEmpty(edit)) {
            edit.setSnpzbh(bo.getSnpzbh());
            edit.setWjj(bo.getWjj());
            edit.setSnjcd(bo.getSnjcd());
            edit.setMsl(bo.getMsl());
            edit.setHjTemp(bo.getHjTemp());
            edit.setJyTemp(bo.getJyTemp());
            edit.setTgCondition(bo.getTgCondition());
            edit.setMjCondition(bo.getMjCondition());
            edit.setFileUrl(bo.getFileUrl());
            edit.setFileId(bo.getFileId());
            edit.setSgDate(bo.getSgDate());
            edit.setUpdateUser(LoginHelper.getUserId().toString());
            edit.setUpdateDate(new Date());
            baseMapper.updateById(edit);
        } else {
            edit = new ZjGroutingReportEdit();
            edit.setSnpzbh(bo.getSnpzbh());
            edit.setWjj(bo.getWjj());
            edit.setSnjcd(bo.getSnjcd());
            edit.setMsl(bo.getMsl());
            edit.setHjTemp(bo.getHjTemp());
            edit.setJyTemp(bo.getJyTemp());
            edit.setTgCondition(bo.getTgCondition());
            edit.setMjCondition(bo.getMjCondition());
            edit.setFileUrl(bo.getFileUrl());
            edit.setFileId(bo.getFileId());
            edit.setSgDate(bo.getSgDate());
            edit.setCreateUser(LoginHelper.getUserId().toString());
            edit.setCreateDate(new Date());
            edit.setBusinessId(bo.getId());
            edit.setDelFlag("0");
            baseMapper.insert(edit);
        }
        if (ObjectUtils.isNotEmpty(bo.getList())) {
            // 修改record表的步骤参数(孔道编号)
            for (GroutingResultVo vo : bo.getList()) {
                if (ObjectUtils.isNotEmpty(vo) && ObjectUtils.isNotEmpty(vo.getLinkCode())) {
                    // 根据linkCode查询record表
                    QueryWrapper<GroutingRecord> wrapper2 = new QueryWrapper<>();
                    wrapper2.eq("link_code", vo.getLinkCode());
                    GroutingRecord groutingRecord = groutingRecordMapper.selectOne(wrapper2);
                    groutingRecord.setStepParameters(ObjectUtils.isNotEmpty(vo.getStepParameters()) ? vo.getStepParameters() : null );
                    groutingRecordMapper.updateById(groutingRecord);
                    // 保存result表的通过情况和冒浆情况
                    GroutingResult bean = BeanUtil.toBean(vo, GroutingResult.class);
                    groutingResultMapper.updateById(bean);
                }
            }
        }
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjGroutingReportEdit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压浆报编辑信息保存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
